package com.smart.web;

import com.smart.domain.User;
import com.smart.service.CartService;
import com.smart.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.Date;

/**
 * Created by Jack on 2015/1/2.
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private CartService cartService;

    @RequestMapping(value="login",produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String getUserByType(HttpServletRequest request,HttpServletResponse response,String username,String password) throws  Exception{
       System.out.println(username+" "+password);
        boolean isvalid = userService.hasMatchUser(Integer.valueOf(username),password);
        System.out.println(isvalid+" ");
        String userInfo = "";
        if (isvalid){
            System.out.println("success");
        userInfo = JSONObject.fromObject(userService.getUserInfo(Integer.valueOf(username))).toString();
            System.out.println(userInfo+" ");
           /* if (cartService.getExistUnFinishCartid(Integer.valueOf(username))>0)
            {
                System.out.println("存在购物车"+cartService.getExistUnFinishCartid(Integer.valueOf(username)));
            }else {
           if (cartService.createCart(Integer.valueOf(username))!=false){
              System.out.println("创建成功");
           }
            }*/
        }

        return  userInfo;
    }


}
